package com.cci.zkweb.core.model;

import java.util.List;

import org.zkoss.zul.AbstractTreeModel;

import com.cci.zkweb.core.entity.p8.Folder;
import com.cci.zkweb.core.util.SessionUtils;

/**
 * Folder tree model
 * Define the model associated with the 
 * folder navigation tree
 * 
 * @author durzod
 */
public class FolderTreeModel 
extends AbstractTreeModel<Folder> {
	private static final long serialVersionUID = -7204274280500217919L;

	public FolderTreeModel(Folder root) {
		super(root);
	}

	@Override
	public boolean isLeaf(Folder node) {
		return getChildCount(node) == 0;
	}

	@Override
	public Folder getChild(Folder parent, int index) {
		if (parent.getChildren() == null) {
			List<Folder> folderList = SessionUtils.getSessionItem().getP8Service().getSubFolders(parent);
			if(folderList != null) {
				parent.setChildren(folderList);
			}
		}
		if (parent.getChildren() != null) 
			return parent.getChildren().get(index);
		return null;
	}

	@Override
	public int getChildCount(Folder parent) {
		if(parent.getChildren() == null) {
			return SessionUtils.getSessionItem().getP8Service().getSubFoldersCount(parent);
		} else return parent.getChildren().size();
	}
}